﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Forms;
using System.IO;
 
namespace EFITransmissao
{
    public partial class FrmCamera : Form
    {
        //Para Coleta:  12 dígitos (UC MesAno: 123456032012)       
        //Para Externa: 13 dígitos (AnoMesDia '_' HoraMinutosSegundos: 120314_163022)

        public FrmCamera()
        {
            InitializeComponent();
        }

        private string _nmExt;

        private static string _nomenclatura;

        public static string Nomenclatura
        {
            get { return _nomenclatura; }
            set { _nomenclatura = value; }
        }

        public string NmExt
        {
            get { return _nmExt; }
            set { _nmExt = value; }
        }

        public static void ObterNomenclatura(string nomenclatura)
        {            
            try
            {
                _nomenclatura = nomenclatura;                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return;         
        }

        private void bSalvar_Click(object sender, EventArgs e)
        {
            if (pbFoto.Image != null)
            {
                if (Directory.Exists(@".\Program Files\EFITransmissao\Fotos\Coleta\") == false)
                {
                    Directory.CreateDirectory(@".\Program Files\EFITransmissao\Fotos\Coleta\");
                }
                if (Directory.Exists(@".\Program Files\EFITransmissao\Fotos\Externa\") == false)
                {
                    Directory.CreateDirectory(@".\Program Files\EFITransmissao\Fotos\Externa\");
                }

                //Verifica o padrão de diretórios.
                if (_nomenclatura.Length == 12)
                {
                    btFechar.Enabled = false;
                    pbFoto.Image.Save(@".\Program Files\EFITransmissao\Fotos\Coleta\" + _nomenclatura + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                else
                {
                    btFechar.Enabled = true;
                    _nmExt = DateTime.Now.Date.Year.ToString().Substring(2, 2) +
                             DateTime.Now.Date.Month.ToString().PadLeft(2,'0') +
                             DateTime.Now.Date.Day.ToString().PadLeft(2, '0') + "_" +
                             DateTime.Now.Hour.ToString().PadLeft(2, '0') +
                             DateTime.Now.Minute.ToString().PadLeft(2, '0') +
                             DateTime.Now.Second.ToString().PadLeft(2, '0');

                    pbFoto.Image.Save(@".\Program Files\EFITransmissao\Fotos\Externa\" + _nmExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                }

                if (MessageBox.Show("Foto salva com sucesso." + "Deseja registrar outra foto?", "Atenção!", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    CapturarFoto();
                }
                else
                {

                    this.Close();
                }
            }
        }

        private void btFechar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btIniciar_Click(object sender, EventArgs e)
        {
            CapturarFoto();
        }

        private void CapturarFoto()
        {
            CameraCaptureDialog ccd = new CameraCaptureDialog();
            ccd.Resolution = new Size(640, 480);
            ccd.Mode = CameraCaptureMode.Still;
            ccd.ShowDialog();

            if (ccd.FileName != string.Empty)
            {
                pbFoto.Image = new Bitmap(ccd.FileName);
                bSalvar.Enabled = true;
            }

            ccd.Dispose();
        }

        private void FrmCamera_Load(object sender, EventArgs e)
        {
            CapturarFoto();
            
        }
        
    }
}